package org.wiztools.te.plugin;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.wiztools.te.TEMod;
import org.wiztools.te.TEProcess;

public class FindAndReplace implements TEProcess{
	private TEMod tem;
	
	private Container ct;
	private JComboBox jcb_search, jcb_replace;
	private JButton jb_find, jb_replace, jb_cancel;
	
	public void setTEMod(TEMod tem){
		this.tem = tem;
	}
	
	public void init(){
		ct = new JPanel();
		ct.setLayout(new BorderLayout());

		JPanel jp_center = new JPanel();
		jp_center.setLayout(new GridLayout(2,2));
		jp_center.add(new JLabel("Search"));
		jcb_search = new JComboBox();
		jcb_search.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					find(tem.getCaretPosition());
				}
			}
		);
		jcb_search.setEditable(true);
		jp_center.add(jcb_search);
		jp_center.add(new JLabel("Replace"));
		jcb_replace = new JComboBox();
		jcb_replace.setEditable(true);
		jp_center.add(jcb_replace);
		
		ct.add(jp_center, BorderLayout.CENTER);

		JPanel jp_south = new JPanel();
		jp_south.setLayout(new FlowLayout());
		jb_find = new JButton("Find");
		jb_find.setMnemonic('F');
		jb_replace = new JButton("Replace And Find");
		jb_replace.setMnemonic('R');
		jb_cancel = new JButton("Cancel");
		jb_cancel.setMnemonic('C');
		jb_find.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					find(tem.getCaretPosition());
				}
			}
		);
		jb_replace.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					replace();
				}
			}
		);
		jb_cancel.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					tem.closeDialog();
				}
			}
		);
		jp_south.add(jb_find);
		jp_south.add(jb_replace);
		jp_south.add(jb_cancel);
		ct.add(jp_south, BorderLayout.SOUTH);
	}
	
	private void find(int curPos){
		String str = tem.getText();
		String val = (String)jcb_search.getSelectedItem();
		if(val==null){
			Toolkit.getDefaultToolkit().beep();
			return;
		}
		addToJCB(jcb_search, val);
		int findPos = str.indexOf(val, curPos);
		if(findPos<0){
			Toolkit.getDefaultToolkit().beep();
		}
		else{
			tem.setSelectionStart(findPos);
			tem.setSelectionEnd(findPos+val.length());
		}
	}
	private void replace(){
		String val1 = (String)jcb_search.getSelectedItem();
		String val2 = (String)jcb_replace.getSelectedItem();
		if(val1==null || val2==null){
			Toolkit.getDefaultToolkit().beep();
			return;
		}
		addToJCB(jcb_replace, val2);
		if(!val1.equals(tem.getSelectedText())){
			Toolkit.getDefaultToolkit().beep();
			return;
		}
		tem.replaceSelection(val2);
		find(tem.getCaretPosition()+val1.length());
	}
	private void addToJCB(JComboBox jcb, String str){
		final int max = 10;
		int count = jcb.getItemCount();
		for(int i=0;i<count;i++){
			String val = (String)jcb.getItemAt(i);
			if(val==null){
				continue;
			}
			if(str.equals(val)){
				return;
			}
		}
		if(count>=max){
			jcb.removeItemAt(max-1);
		}
		jcb.insertItemAt(str, 0);
	}
	public void action(){
		tem.dispDialog(ct, "FindAndReplace");
	}
	
	public void destroy(){
		// do nothing!
	}
}
